"""


"""
from django.conf import settings
from django.http import HttpResponse
from itsdangerous import TimedJSONWebSignatureSerializer


# 加密生成verify_url
from users.models import User


def generate_verify_url(user):

    token = {"user_id": user.id, "email": user.email}

    s1 = TimedJSONWebSignatureSerializer(secret_key=settings.SECRET_KEY, expires_in=30*60)

    s_token = s1.dumps(token).decode()

    verify_url = "%s?token=%s" % (settings.EMAIL_VERIFY_URL, s_token)

    return verify_url


# 把token解密并返回一个user对象
def decipher_token(s_token):

    s1 = TimedJSONWebSignatureSerializer(secret_key=settings.SECRET_KEY, expires_in=30*60)
    # 解码
    try:
        token = s1.loads(s_token)

        print(token)
        print(token.get("user_id"))

        # 用userid获取对象
        user = User.objects.get(id=token.get("user_id"))

    except Exception:
        return None

    return user
